package order.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import common.model.order.Orders;
import common.model.result.Result;
import common.model.result.ResultCodeEnum;
import order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import common.dto.userdto.AddressDTO;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    
    @Autowired
    private ObjectMapper objectMapper;

    //创建订单
    @RequestMapping("/create")
    public Result orderCreate(@RequestHeader("uid") Long uid, @RequestBody Map<String,Object> request) {
        try {
            // 获取地址信息并转换为 AddressDTO
            Map<String, Object> addressMap = (Map<String, Object>) request.get("address");
            AddressDTO addressDTO = objectMapper.convertValue(addressMap, AddressDTO.class);
            
            // 获取选中的购物车id列表
            @SuppressWarnings("unchecked")
            List<String> cartIds = (List<String>) request.get("cartIds");
            
            // 获取优惠券id
            List<String> couponCodes = (List<String>) request.get("couponCodes");  
            
            //创建订单
            Result newOrder = orderService.createOrder(uid, addressDTO, cartIds, couponCodes);
            Orders order = (Orders) newOrder.getData();
            if (order == null) {
                return Result.failure(ResultCodeEnum.FAIL,"订单为空");
            }
            Long orderId = order.getId();
            //返回订单id
            return Result.success(orderId);
        } catch (Exception e) {
            return Result.failure(ResultCodeEnum.FAIL, "创建订单失败：" + e.getMessage());
        }
    }
    //获取订单列表
    @RequestMapping("/list")
    public Result orderList(@RequestHeader("uid") Long uid) {
        
        return orderService.getOrders(uid);
    }
    //根据订单id查询订单
    @GetMapping("/status/{orderId}")
    public Result orderStatus(@PathVariable("orderId") Long orderId) {
        return orderService.getOrderById(orderId);
    }
    //根据订单状态查询订单
    @GetMapping("/status")
    public Result orderStatus(@RequestParam("status") Integer status, @RequestHeader("uid") Long uid) {
        return orderService.getOrdersByStatus(status, uid);
    }

    //模拟商家发货
    @PostMapping("/ship/{orderId}")
    public Result orderShip(@PathVariable("orderId") Long orderId) {
        return orderService.shipOrder(orderId);
    }

    //模拟已经收货订单完成
    @PostMapping("/finish/{orderId}")
    public Result orderFinish(@PathVariable("orderId") Long orderId) {
        return orderService.finishOrder(orderId);
    }

    //取消订单
    @PostMapping("/cancel/{orderId}")
    public Result orderCancel(@PathVariable("orderId") Long orderId) {
        return orderService.cancelOrder(orderId);
    }
    //删除订单
    @DeleteMapping("/delete")
    public Result orderDelete(@RequestParam("oid") Long oid) {
        orderService.deleteOrder(oid);
        return Result.success();
    }

    //根据spuId查询订单id
    @GetMapping("/orderId")
    public Result getOrderIdBySpuId(@RequestHeader("uid") Long userId, @RequestParam("spuId") Long spuId) {
        return orderService.getOrderIdBySpuId(userId, spuId);
    }

    //根据订单项id获取物流信息
    @GetMapping("/logistics/{orderItemId}")
    public Result getLogisticsByOrderItemId(@PathVariable("orderItemId") Long orderItemId) {
        return orderService.getLogisticsByOrderItemId(orderItemId);
    }

    //后台
    //根据时间单位获取销售趋势数据
    @GetMapping("/jiang/salesTrend")
    public Result salesTrend(@RequestParam("timeUnit") String timeUnit) {
        return orderService.getSalesTrend(timeUnit);
    }
    //获取最近的订单
    @GetMapping("/jiang/recentOrders")
    public Result recentOrders() {
        return orderService.getRecentOrders();
    }
    //获取订单列表
    @GetMapping("/jiang/list")
    public Result orderList() {
        return orderService.getOrdersList();
    }
    //获取订单详情
    @GetMapping("/jiang/detail")
    public Result orderDetail(@RequestParam("orderId") Long orderId) {
        return orderService.getOrderById(orderId);
    }
    //发货
    @PostMapping("/jiang/ship")
    public Result shipOrder(@RequestParam("orderId") Long orderId) {
        return orderService.shipOrder(orderId);
    }
    //取消订单
    @PostMapping("/jiang/cancel")
    public Result cancelOrder(@RequestParam("orderId") Long orderId) {
        return orderService.cancelOrder(orderId);
    }
}
